//
//  YSCategoryHeaderView.m
//  Yueshi
//
//  Created by Northwan on 15/7/21.
//  Copyright (c) 2015年 悦食中国. All rights reserved.
//

#import "YSCategoryHeaderView.h"
#import "SGFocusImageFrame.h"
#import "SGFocusImageItem.h"
#import "YSAdvert.h"


@interface YSCategoryHeaderView()<SGFocusImageFrameDelegate>
@property (nonatomic,strong)SGFocusImageFrame *bannerView;
@property (nonatomic,strong)NSMutableArray *itemArray;
@end
@implementation YSCategoryHeaderView

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self setFrame:CGRectMake(0, 0, SCREENW, SCREENW/1.5)];
    }
    return self;
}


-(void)setBanner:(NSArray *)banner{
    [self inflateTopDataWithArray:banner];
}

-(void)inflateTopDataWithArray:(NSArray *)bannerArray{
    [self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    if(!bannerArray.count)return;
    NSMutableArray *itemArray = [NSMutableArray arrayWithCapacity:bannerArray.count+2];
    for (int i = 0; i < bannerArray.count; i++)
    {
        NSString *img =  ((YSAdvert *)bannerArray[i]).advertImg;
        NSString *title =  ((YSAdvert *)bannerArray[i]).advertUrl;
        SGFocusImageItem *item = [[SGFocusImageItem alloc] initWithTitle:title image:img tag:i];
        [itemArray addObject:item];
    }
    
    if(bannerArray.count>1){
        NSString *image =  ((YSAdvert *)[bannerArray lastObject]).advertImg;
        NSString *title =  ((YSAdvert *)[bannerArray lastObject]).advertUrl;
        SGFocusImageItem *startItem = [[SGFocusImageItem alloc] initWithTitle:title image:image tag:bannerArray.count-1];
        [itemArray insertObject:startItem atIndex:0];
        
        NSString *firstimage =  ((YSAdvert *)[bannerArray firstObject]).advertImg;
        NSString *firsttitle =  ((YSAdvert *)[bannerArray firstObject]).advertUrl;
        SGFocusImageItem *endItem = [[SGFocusImageItem alloc] initWithTitle:firsttitle image:firstimage tag:bannerArray.count-1];
        [itemArray addObject:endItem];
    }
    
    SGFocusImageFrame *bannerView = [[SGFocusImageFrame alloc] initWithFrame:CGRectMake(0, 0, SCREENW,SCREENW/1.5) delegate:self imageItems:itemArray isAuto:YES];
    [bannerView scrollToIndex:0];
    [self addSubview:bannerView];
}

-(void)foucusImageFrame:(SGFocusImageFrame *)imageFrame didSelectItem:(SGFocusImageItem *)item{
    NSLog(@"%s,%@",__FUNCTION__,item.title);
    [Utils dealUrlWithController:self.viewController url:item.title fromWebView:NO];
}

@end
